<template>
  <div>
    <!-- 开始 -->
    <!-- 面包屑导航 -->
        <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: 'index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--按钮 -->
    <el-card>
      <el-row>
        <el-col
          ><el-button
            type="primary"
            class="AddUser"
            @click="dialogFormVisible = true"
            >添加角色</el-button
          >
          <el-dialog title="添加角色" :visible.sync="dialogFormVisible">
            <el-form :model="formRole">
              <el-form-item label="角色名称">
                <el-input
                  autocomplete="off"
                  v-model="formRole.roleName"
                ></el-input>
              </el-form-item>
              <el-form-item label="角色描述">
                <el-input
                  autocomplete="off"
                  v-model="formRole.roleDesc"
                ></el-input>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible = false">取 消</el-button>
              <el-button type="primary" @click="AddUserValue()"
                >确 定</el-button
              >
            </div>
          </el-dialog>
        </el-col>
      </el-row>
      <!-- 角色区域 -->
      <el-table :data="roleList" style="width: 100%">
        <!-- 展开内容 -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
              v-for="(item, key) in scope.row.children"
              :key="key"
              class="RowTop"
            >
              <!-- 第一层 -->
              <el-col :span="5">
                <el-tag closable @close="TagSelect(scope.row, item.id)">
                  {{ item.authName }}
                </el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 第2 层 -->
              <el-col :span="19">
                <el-row
                  v-for="(item1, key) in item.children"
                  :key="key"
                  class="RowMiddle"
                  closable
                  @close="TagSelect(scope.row, item1.id)"
                >
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="TagSelect(scope.row, item1.id)"
                    >
                      {{ item1.authName }}
                    </el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      type="warning"
                      v-for="(item2, key) in item1.children"
                      :key="key"
                      closable
                      @close="TagSelect(scope.row, item2.id)"
                    >
                      {{ item2.authName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index"> </el-table-column>
        <el-table-column label="角色名称" prop="roleName"> </el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"> </el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="getCompileRoleValue(scope.row)"
              >编辑</el-button
            >
            <el-dialog title="修改角色" :visible.sync="compileRoleValue">
              <el-form :model="compileRole">
                <el-form-item label="角色名称">
                  <el-input
                    autocomplete="off"
                    v-model="compileRole.roleName"
                  ></el-input>
                </el-form-item>
                <el-form-item label="角色描述">
                  <el-input
                    autocomplete="off"
                    v-model="compileRole.roleDesc"
                  ></el-input>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="compileRoleValue = false">取 消</el-button>
                <el-button type="primary" @click="SetCompileRole(scope.row)"
                  >确 定</el-button
                >
              </div>
            </el-dialog>
            <el-button
              type="danger"
              size="mini"
              @click="deleteIdRole(scope.row.id)"
              >删除</el-button
            >
            <el-button
              type="warning"
              size="mini"
              @click="privileges(scope.row)"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分配权限 -->
    <template>
      <div>
        <el-dialog
          title="分配权限"
          :visible.sync="setPrivileges"
          width="50%"
          @close="setRightClosed"
        >
          <!-- 树形 -->
          <el-tree
            :data="rightsList"
            :props="Props"
            default-expand-all
            node-key="id"
            show-checkbox
            :default-checked-keys="Keys"
            ref="treeRef"
          ></el-tree>
          <span slot="footer" class="dialog-footer">
            <el-button @click="setPrivileges = false">取 消</el-button>
            <el-button type="primary" @click="setRigthAll">确 定</el-button>
          </span>
        </el-dialog>
      </div>
    </template>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 角色列表
      roleList: [],
      // 分配权限展开页面
      setPrivileges: false,
      rightsList: [],
      Props: {
        label: "authName",
        children: "children",
      },
      Keys: [],
      // 分配角色id
      roleIdS: "",
      // 添加角色
      dialogFormVisible: false,
      // 编辑角色
      compileRoleValue: false,
      formRole: {
        roleName: "",
        roleDesc: "",
      },
      compileRole: {},
    };
  },
  methods: {
    // 获取编辑人员信息
    getCompileRoleValue(getRoleValue) {
      this.compileRole = getRoleValue;
      this.compileRoleValue = true;
    },
    // 编辑人员
    SetCompileRole() {
      this.$Req
        .put(`/roles/${this.compileRole.id}`, this.compileRole)
        .then((res) => {
   
          const { data, meta } = res.data;
          if (meta.status == 200) {
            this.$message({
              type: "success",
              message: "编辑人员信息成功",
            });
          }
        });
      this.compileRoleValue = false;
    },
    // 添加角色
    AddUserValue() {
      this.$Req.post(`/roles`, this.formRole).then((res) => {
   
        const { data, meta } = res.data;

        if (meta.status === 201) {
          this.$message({
            type: "success",
            message: meta.msg,
          });
   this.getUser();
        }
        this.dialogFormVisible = false;
     
      });
    },
    // 删除角色
    deleteIdRole(DeRoleId) {
  
      this.$confirm("此操作将永久删除此人员, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          this.$Req.delete(`/roles/${DeRoleId}`).then((res) => {
      
            const { data, meta } = res.data;
            if (meta.status === 200) {
              this.$message({
                showClose: true,
                message: meta.msg,
                type: "success",
              });
              this.getUser();
            } else {
              this.$message.error(meta.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 点击分配角色  权限
    async setRigthAll() {
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys(),
      ];

      const idArr = keys.join(",");

      await this.$Req
        .post(`roles/${this.roleIdS}/rights`, { rids: idArr })
        .then((res) => {
          const { data, meta } = res.data;

          if (meta.status == 200) {
            this.$message({
              type: "success",
              message: meta.msg,
            });
          }
        });
      this.getUser();
      this.setPrivileges = false;
    },
    // 监听分配权限
    setRightClosed() {
      this.Keys = [];
    },
    // 获取当前角色的所有权限 保存到Keys
    getKeys(node, arr) {
      if (!node.children) {
        return arr.push(node.id);
      }
      node.children.forEach((element) => {
        this.getKeys(element, arr);
      });
    },
    // 分配权限
    async privileges(role) {

      this.roleIdS = role.id;
      this.setPrivileges = true;
      await this.$Req.get(`rights/tree`).then((res) => {
        const { data, meta } = res.data;
        if (meta.status !== 200) {
          this.$message({
            type: "success",
            message: meta.msg,
          });
        }
        // 获取到的用户信息保存起来
        this.rightsList = data;
 
        // 获取用户三级权限
        this.getKeys(role, this.Keys);
      });
    },
    //  删除
    async TagSelect(roleId, rightId) {
      await this.$confirm("此操作将永久删除该权限, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then((res) => {
          if (res == "confirm") {
            this.$Req
              .delete(`roles/${roleId.id}/rights/${rightId}`)
              .then((r) => {
                const { data, meta } = r.data;
                if (meta.status == 200) {
                  roleId.children = data;
                }
              });
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    getUser() {
      this.$Req.get("roles").then((res) => {
        const { data, meta } = res.data;
        this.roleList = data;

      });
    },
  },
  mounted() {
    // 获取角色列表
    this.getUser();
  },
};
</script>

<style>
.AddUser {
  float: left;
}
.RowTop {
  border-bottom: 1px solid #ccc;
  margin: 7px;
}
.RowMiddle {
  border-bottom: 1px solid #ccc;
  margin: 7px;
}
</style>